package com.wshare.dao.impl;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.wshare.dao.interfaces.IAccountDao;
import com.wshare.entity.Account;

@Repository
@Transactional
public class AccountDaoImpl implements IAccountDao{

	@Autowired
	SessionFactory sessionFactory;
	
	@Override
	public List<Account> getByCol(String col, Object val) {
		Session session = sessionFactory.openSession();
		List lAccount = session
				.createCriteria(Account.class)
				.add(Restrictions.eq(col,val))
				.list();
		session.close();
		return lAccount;
	}

	@Override
	public List<Account> getAll() {
		Session session = sessionFactory.openSession();
		List lAccount = session
				.createCriteria(Account.class)
				.addOrder(Order.asc("id"))
				.list();
		session.close();
		return lAccount;
	}

	@Override
	public int addAccount(Account acc) {
		return (Integer) sessionFactory.getCurrentSession().save(acc);
	}

}
